package cn.kgc.service.impl;

import cn.kgc.dao.UserDao;
import cn.kgc.pojo.*;
import cn.kgc.service.userService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 张鹏飞
 * @date: 2022/1/24
 * @description:
 */
@Service
public class UserServiceImpl implements userService {

    @Resource
    UserDao userDao;

    @Override
    public Map<String, Object> getMenu(String userCode, String userPwd) {
        User user = userDao.getLogin(userCode, userPwd);
        Map<String, Object> map = new LinkedHashMap<>();
        ArrayList<String> sortList = new ArrayList<>();//去重,控制内层循环次数
        for (Menu menu : user.getMenu()) {
            if (!sortList.contains(menu.getSname())) {
                ArrayList<ZpfUtil> menuList = new ArrayList<>();//存放二级分类的集合
                sortList.add(menu.getSname());
                for (Menu userMenu : user.getMenu()) {
                    if (userMenu.getSname().equals(menu.getSname())) {
                        ZpfUtil zu=new ZpfUtil();
                        zu.setMName(userMenu.getMname());
                        zu.setMUrl(userMenu.getMurl());
                        menuList.add(zu);
                    }
                }
                map.put(menu.getSname(), menuList);//键对应一级分类，值对应二级分类
            }
        }
        return map;
    }

    @Override
    public Page<User> selUsers(String userName) {
        Page<User> page = userDao.selUsers(userName);
        return page;
    }

    @Override
    public boolean updRole(int rId, int id) {
        if (userDao.updRole(rId, id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updPassword(String userCode, String oldPassword, String newPassword) {
        if (userDao.login(userCode,oldPassword)>0&&userDao.updPassword(userCode, newPassword) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<Role> selRoles() {
        return userDao.selRoles();
    }

    @Override
    public boolean login(String userCode, String userPwd) {
        if (userDao.login(userCode, userPwd) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> selMenu() {
        return userDao.selMenu();
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public boolean updRole_Menu(Integer rId,  List<Integer> mIdList) {
        try {
            int a=userDao.updRole_Menu(rId,null,1);
            if(a<=0){
                return false;
            }
            for (int i = 0; i <mIdList.size(); i++) {
                int j=userDao.updRole_Menu(rId,mIdList.get(i),0);
                if(j<=0){
                    return false;
                }
            }

        } catch (NoTransactionException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    @Override
    public String zpf_getUserName(String userCode, String userPwd) {
        return userDao.zpf_getUserName(userCode,userPwd);
    }

    @Override
    public String zpf_getUserRole(String userCode, String userPwd) {
        return userDao.zpf_getUserRole(userCode,userPwd);
    }

    @Override
    public List<Menu> selMenuByRid(Integer rId) {
        return userDao.selMenuByRid(rId);
    }

    @Override
    public List<Role_Menu> selAllMenuByRid(Integer rId) {
        return userDao.selAllMenuByRid(rId);
    }


}
